<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function register(){
		$data['view'] = "";
		$this->load->helper('form');
	
		$this->load->view('correction/header',$data);
		$this->load->view('correction/register');
		$this->load->view('correction/footer');
	}
	
	public function login(){
		$sql = "SELECT a.*,b.groupid FROM ci_user AS a,ci_user_plus AS b WHERE a.username = ? AND a.password = ? AND a.uid = b.uid";
		$query = $this->db->query($sql,array($this->input->post('username'),sha1($this->input->post('pwd'))));
		$row = $query->row();
		if($query->num_rows()>0){
			$array = array(
				'uid'=>$row->uid,
				'username'=>$row->username,
				'is_login'=>true,
				'groupid'=>$row->groupid
			);
			$this->session->set_userdata($array);
			$this->load->view('correction/message',array('msg'=>'Log in successed!','timer'=>3,'redirect'=>site_url('index.php/correction/icorrection')));
		}else{
			$this->load->view('correction/message',array('msg'=>'Log in failed!','timer'=>3,'redirect'=>site_url('index.php/user/register')));
		}
	}
	
	public function logout(){
		$this->session->sess_destroy();
		$this->load->view('correction/message',array('msg'=>'Log out successed!','timer'=>3,'redirect'=>base_url()));	
	}
	
	public function add(){
		$query = $this->db->get_where("ci_user",array('username'=>$this->input->post('username'),'email'=>$this->input->post('email')));
		if(count($query->result())>0){
			redirect(base_url().'index.php/user/register');
		}else{
			$this->load->model('User_model');
			$newuid = $this->User_model->add();
			if($newuid){
				$array = array(
					'uid'=>$newuid,
					'username'=>$this->input->post('username'),
					'is_login'=>true,
					'groupid'=>2
				);
				$this->session->set_userdata($array);
				$this->load->view('correction/message',array('msg'=>'Regist successed!','timer'=>3,'redirect'=>base_url()));
			}else{
				$this->load->view('correction/message',array('msg'=>'Regist failed!','timer'=>3,'redirect'=>site_url('index.php/user/register')));
			}
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */